package com.guanyu.u2cache.caffeine;

import cn.hutool.core.util.ObjectUtil;
import com.github.benmanes.caffeine.cache.Cache;
import com.guanyu.u2cache.cache.Level1Cache;
import com.guanyu.u2cache.manage.ListenerEventManag;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author zhouyunjian
 * @date 2023/11/27 11:08
 **/
@NoArgsConstructor
@Slf4j
public class Level1CacheImpl implements Level1Cache {
    //    @Autowired
//    Cache<String, Object> caffeineCache;
//    @Autowired
//    private U2CacheManager manager;


    @Override
    public Object getCache(String[] eventNames, String key, long expireTime, TimeUnit timeUnit) {
        for (String eventName : eventNames) {
//            Cache<String, Object> cache = U2CacheManager.getSingleton().getCache(eventName);
//            if (null == cache) {
//                continue;
//            }
//            log.debug("【缓存获取】一级缓存eventName[{}]下的key[{}]", eventName, key);
//            return cache.getIfPresent(key);
            Object result = U2CacheManager.getSingleton().getCacheByKey(eventName, key, true);
            if (!ObjectUtil.isEmpty(result)) {
                return result;
            }
        }
        return null;
    }

    @Override
    public void putCache(String[] eventNames, String key, Object result, Class returnType, long expireTime, TimeUnit timeUnit) {
        for (String eventName : eventNames) {
            Cache<String, Object> cache = U2CacheManager.getSingleton().getCache(eventName);
            if (null == cache) {
                continue;
            }
            log.debug("【缓存传入】eventName[{}]下的key[{}]", eventName, key);
            cache.policy().expireVariably().ifPresent(e -> {
                if (expireTime <= 0) {
                    e.put(key, result, 9999999L, timeUnit);
                } else {
                    e.put(key, result, expireTime, timeUnit);
                }
                Optional<Duration> expiresAfter = e.getExpiresAfter(key);
                log.debug("剩余可用时间" + expiresAfter.get());
            });
        }
    }

    @Override
    public void clear(String[] eventNames, String key) {
        for (String eventName : eventNames) {
            Cache<String, Object> cache = U2CacheManager.getSingleton().getCache(eventName);
            if (null == cache) {
                continue;
            }
            cache.invalidate(key);
            log.debug("【缓存清理】事件[{}]key[{}]被清空", eventNames, key);
        }
    }

    @Override
    public boolean isExistKey(String[] eventNames, String key) {
        for (String eventName : eventNames) {
            Cache<String, Object> cache = U2CacheManager.getSingleton().getCache(eventName);
            if (null == cache) {
                continue;
            }
            log.debug("【缓存key存在】一级缓存eventName[{}]下的key[{}]是否存在", eventName, key);
            return ObjectUtil.isNotEmpty(cache.getIfPresent(key));
        }
        return false;
    }

    @Override
    public void clear(String[] listenEventName) {
        log.debug("删除一级缓存事件信息[{}]", listenEventName);
        for (String eventName : listenEventName) {
            String[] cacheNames = ListenerEventManag.getSingleton().getCacheNames(eventName);
            log.debug("删除一级缓存[{}]", cacheNames);
            for (String cacheName : cacheNames) {
                Cache<String, Object> cache = (Cache<String, Object>) U2CacheManager.getSingleton().getCache(cacheName);
                cache.invalidateAll();
            }
        }
    }
}
